package com.esm.daoimpl;



import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.esm.dao.FuncDao;
import com.esm.utils.JDBCUtil;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;


/**
 * 
 * @author 王文成
 *
 */
public class FuncDaoImpl implements FuncDao{
	
	public String findRoleFidsByrid(int rid) 
	{
		String fids = "";
		StringBuffer  sql = new StringBuffer();
		sql.append("SELECT ems_role.fids");
		sql.append(" FROM ems_role");
		sql.append(" WHERE ems_role.rid=?");
		
		Connection  con = null;
		PreparedStatement pst = null;
		try 
		{
			con = JDBCUtil.getConnection();
			pst = con.prepareStatement(sql.toString());
			pst.setInt(1,rid);
			
			ResultSet  rs = pst.executeQuery();
			while(rs.next())
			{
				fids = rs.getString("fids");
			}
		} 
		catch (SQLException e) 
		{
			e.printStackTrace();
		}
		return fids;
	}
	
	
	public JSONArray findUserFuncsByFidsPid(String fids,int pid) 
	{
		JSONArray jao = new JSONArray();
		StringBuffer  sql = new StringBuffer();		
	
		
		sql.append(" SELECT ems_function.fid,ems_function.fname,ems_function.fstate,ems_function.furl");
		sql.append(" FROM ems_function");
		sql.append(" WHERE ems_function.fid IN ("+fids+")");
		sql.append(" AND ems_function.pid=?");
		Connection  con = null;
		PreparedStatement pst = null;
		try 
		{
			con = JDBCUtil.getConnection();
			pst = con.prepareStatement(sql.toString());
			pst.setInt(1, pid);
			ResultSet  rs = pst.executeQuery();
			while(rs.next())
			{
				JSONObject jo = new JSONObject();
				jo.put("id", rs.getInt("fid"));
				jo.put("text",rs.getString("fname"));
				jo.put("state",rs.getString("fstate"));
				
				JSONObject  attr = new JSONObject();
				attr.put("url", rs.getString("furl"));
				jo.put("attributes", attr);
				jao.add(jo);
			}
		} 
		catch (SQLException e) 
		{
			e.printStackTrace();
		}
		return jao;
	}


	public String findRoleFidsByRname(String rname) {
		// TODO Auto-generated method stub
		String fids=null;
		StringBuffer sql=new StringBuffer();
		sql.append("select ems_role.fids from ems_role");
		sql.append(" where ems_role.rname=?");
		Connection con= JDBCUtil.getConnection();
		try {
			PreparedStatement pst= con.prepareStatement(sql.toString());
			pst.setString(1, rname);
			ResultSet rs=pst.executeQuery();
			while (rs.next()) {
				fids=rs.getString("fids");
				
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return fids;
	}


	public JSONArray findFuncsByFids(String fids) {
		// TODO Auto-generated method stub
		JSONArray jay=new JSONArray();
		StringBuffer sql=new StringBuffer();
		sql.append("select ems_function.fid ,ems_function.fname,ems_function.fstate,ems_function.furl,ems_function.isselect");
		sql.append(" from ems_function where ems_function.fid in ("+fids+")");
		Connection con= JDBCUtil.getConnection();
		try {
			PreparedStatement pst =con.prepareStatement(sql.toString());
			
			ResultSet rs=pst.executeQuery();
			while(rs.next()){
				JSONObject job=new JSONObject();
				job.put("id", rs.getInt("fid"));
				job.put("text", rs.getString("fname"));
				job.put("state", rs.getString("fstate"));
				JSONObject attr=new JSONObject();
				job.put("url", rs.getString("furl"));
				job.put("isselect", rs.getString("isselect"));
				job.put("attributes", attr);
				jay.add(job);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return jay;
	}


	public JSONArray findAllFuncs() {
		// TODO Auto-generated method stub
		JSONArray allfunc=new JSONArray();
		StringBuffer  sql=new StringBuffer();
		sql.append("select ems_function.fid,ems_function.fname,ems_function.fstate,ems_function.furl");
		sql.append(" from ems_function");
		Connection con =JDBCUtil.getConnection();
		try {
			PreparedStatement pst=con.prepareStatement(sql.toString());
			ResultSet rs=pst.executeQuery();
			while (rs.next()) {
				JSONObject jo=new JSONObject();
				jo.put("id", rs.getInt("fid"));
				jo.put("text", rs.getString("fname"));
				jo.put("state", rs.getString("fstate"));
				JSONObject attr=new JSONObject();
				jo.put("url", rs.getString("furl"));
				jo.put("attributes", attr);
				allfunc.add(jo);
				
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return allfunc;
	}


	public boolean updateFidsByRname(String rname,String fids) {
		// TODO Auto-generated method stub
		boolean flag=false;
		StringBuffer sql=new StringBuffer();
		sql.append("update ems_role set ems_role.fids=?");
		sql.append(" where ems_role.rname=?");
		Connection con=JDBCUtil.getConnection();
		try {
			PreparedStatement pst=con.prepareStatement(sql.toString());
			pst.setString(1, fids);
			pst.setString(2, rname);
			int rs=pst.executeUpdate();
			if (rs>0) {
				flag=true;
			}
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return flag;
	}
	
	



}
